package com.audible.library.view.util;

/**
 * This Date and Time utility can help the presentation layer to
 * render the duration of audio book. We are storing audio book length in
 * seconds in database. This class can help us convert seconds into
 * something like "1 hr and 31 mins" formatted string.
 *
 * @author Jigar Shah
 * @version 0.1
 */
public class DateTimeUtil {

    /**
     * The method will convert seconds into meaningful time string.
     * For example, 377777 seconds will be converted into "104 hr and 56 mins".
     * This method can be useful to presentation layer.
     *
     * @param seconds
     * @return String (e.g. xxxx hr and xx mins)
     */
    public static String getTimeDurationStr(int seconds) {

        StringBuilder output = new StringBuilder();
        int hour = seconds / (60 * 60);

        if (hour > 0) {
            output.append(hour);
            output.append(" hr");
        }

        int remainderSec = seconds % (60 * 60);

        int min = remainderSec / 60;

        if (min > 1) {
            if (hour > 0) {
                output.append(" and ");
            }

            output.append(min);
            output.append(" mins");
        } else if (min == 1) {

            if (hour > 0) {
                output.append(" and ");
            }

            output.append(min);
            output.append(" min");
        }

        return output.toString();
    }

    public static void main(String[] args) {
        System.out.println(DateTimeUtil.getTimeDurationStr(377777));
    }
}
